package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;


    /**
     * 分页查询用户列表
     * 参数：page,size
     * 业务逻辑：调用service分页查询数据
     * 问题：查询用户需要根据当前登录人所属的企业id查询
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10") int size) {

        PageInfo info = userService.findAll(getLoginCompanyId(), page, size);

        request.setAttribute("page", info);

        return "system/user/user-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        List<Dept> deptList = deptService.findAll(getLoginCompanyId());
        request.setAttribute("deptList", deptList);
        return "system/user/user-add";
    }

    @RequestMapping("/edit")
    public String edit(User user) {
        user.setCompanyId(getLoginCompanyId());
        user.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(user.getId())) {
            userService.save(user);
        } else {
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        User user = userService.findById(id);
        request.setAttribute("user", user);
        List<Dept> deptList = deptService.findAll(getLoginCompanyId());
        request.setAttribute("deptList", deptList);
        return "system/user/user-update";
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        userService.delete(id);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping("/roleList")
    public String roleList(String id) {
        //1、根据id查询用户
        User user = userService.findById(id);
        request.setAttribute("user",user);
        //2、查询所有角色
        List<Role> roleList = roleService.findAll(getLoginCompanyId());
        request.setAttribute("roleList",roleList);
        //3、根据用户id，查询此用户所具有的所有角色id
        List<String> roles = userService.findRolesByUserId(id);
        String userRoleStr = "";
        for (String roleId : roles) {
            userRoleStr += roleId + " ";
        }
        request.setAttribute("userRoleStr",userRoleStr);
        //4、跳转页面
        return "system/user/user-role";
    }

    /**
     * 实现对用户分配角色
     *  请求：/system/user/changeRole.do
     *  参数：
     *      1、userid 用户id
     *      2、roleIds 勾选的分配角色id的数组
     *  业务：
     *      1、调用service
     *      2、重定向
     */
    @RequestMapping("/changeRole")
    public String changeRole(String userid,String [] roleIds) {
        //1、调用service
        userService.changeRole(userid,roleIds);
        //2、重定向
        return "redirect:/system/user/list.do";
    }
}
